﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security;
using System.Text;
using System.Threading.Tasks;

namespace Inovout
{
    [SuppressUnmanagedCodeSecurity, SecurityCritical]
    public static class Win32Native
    {
        [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
        private static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword,
            int dwLogonType, int dwLogonProvider, out IntPtr tokenHandle);
        private const int LOGON32_PROVIDER_DEFAULT = 0;
        private const int LOGON32_LOGON_INTERACTIVE = 2;

        public static bool AuthenticateUser(string userName, string password)
        {
            return AuthenticateUser(Environment.MachineName, userName, password);
        }
        public static bool AuthenticateUser(string domainName, string userName, string password)
        {
            ArgumentAssert.IsNotDefault(userName, "userName");
            ArgumentAssert.IsNotDefault(password, "password");
            IntPtr tokenHandle;
            return LogonUser(userName, domainName, password,
               LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, out tokenHandle);
        }
    }
}
